package org.gov.vis.master.controller;

import java.util.List;

import org.gov.vis.common.exceptions.InvalidInputException;
import org.gov.vis.common.exceptions.SystemException;
import org.gov.vis.master.dao.DepartmentDao;
import org.gov.vis.master.dao.DepartmentDaoImpl;
import org.gov.vis.master.dto.DeptDto;
import org.gov.vis.util.GuiConstants;
import org.gov.vis.util.ValidatorUtils;

public class DepartmentCtlrImpl implements DepartmentCtlr{


	/**
	 * Method called to Add or Edit the Department
	 * @param deptDto
	 * @throws Exception
	 */
	@Override
	public void addOrEditDepartment(final DeptDto _deptDto) throws Exception {
		try{
			/*
			 * Below set of validations check for Required/Mandatory fields
			 */
			String _deptCodeErr = ValidatorUtils.validateString(rb.getString("dialog_label_dept_code"), _deptDto.getCode(), 1, 20, true);
			if(_deptCodeErr != null)
				errorMsgs.put(rb.getString("dialog_label_dept_code").toUpperCase(), _deptCodeErr);

			String _deptNameInstErr = ValidatorUtils.validateString(rb.getString("dialog_label_dept_institution"), _deptDto.getInstitution(), 5, 50, true);
			if(_deptNameInstErr != null)
				errorMsgs.put(rb.getString("dialog_label_dept_institution").toUpperCase(), _deptNameInstErr);

			if (errorMsgs != null && !errorMsgs.isEmpty())
				throw new InvalidInputException();

			/*
			 * Below set of validations check for duplicate records
			 */
			//check if there exists andept Dept Object with the same Code
			//If Code has been duplicated, then throw the alert
			DeptDto deptDto = null;
			if(_deptDto.getDeptId() > 0){
				//get the DeptDto on the passed Code name
				deptDto = this.getDepartmentOnCode(_deptDto.getCode());
				if(deptDto != null && deptDto.getDeptId() != _deptDto.getDeptId()){
					errorMsgs.put(rb.getString("dialog_label_dept_code").toUpperCase(), rb.getString(GuiConstants.MASTER_DUPLICATE_OTHER_MSG));
				}
			}//end if
			else{
				//get the DeptDto on the passed Code name
				deptDto = this.getDepartmentOnCode(_deptDto.getCode());
				if(deptDto != null){
					errorMsgs.put(rb.getString("dialog_label_dept_code").toUpperCase(), rb.getString(GuiConstants.MASTER_DUPLICATE_DEPARTMENT_MSG));
				}
			}//end else

			if (errorMsgs != null && !errorMsgs.isEmpty())
				throw new InvalidInputException();


			final DepartmentDao _deptDao = new DepartmentDaoImpl();

			//add or edit the Department
			_deptDao.addOrEditDept(_deptDto);
		}
		catch(InvalidInputException ie){
			throw ie;
		}
		catch (Exception ex) {
			ex.printStackTrace();
			if(_deptDto.getDeptId() > 0){
				throw new SystemException(rb.getString(GuiConstants.MASTER_EDIT_DEPARTMENT_EXCEPTION));
			}
			else{
				throw new SystemException(rb.getString(GuiConstants.MASTER_ADD_DEPARTMENT_EXCEPTION));
			}
		}
	}//end addOrEditDepartment

	/**
	 * Method called to delete the Department Object based on the passed dept Id
	 * @param _deptId
	 * @throws Exception
	 */
	@Override
	public void deleteDepartment(final long _deptId) throws Exception {
		try{
			final DepartmentDao _deptDao = new DepartmentDaoImpl();
			_deptDao.deleteDept(_deptId);
		}
		catch (Exception ex) {
			throw new SystemException(rb.getString(GuiConstants.MASTER_DELETE_DEPARTMENT_EXCEPTION));
		}
	}//end deleteDepartment

	/**
	 * Method called to get the Department based on the passed primary key Id
	 * @param _deptId
	 * @return
	 * @throws Exception
	 */
	@Override
	public DeptDto getDepartment(final long _deptId) throws Exception {
		try{
			final DepartmentDao _deptDao = new DepartmentDaoImpl();
			DeptDto _deptDto = new DeptDto();
			_deptDto.setDeptId(_deptId);

			DeptDto deptDto = null;
			List<DeptDto> _deptList = _deptDao.getDepartments(_deptDto);
			if(_deptList != null && !_deptList.isEmpty()){
				deptDto = _deptList.get(0);
			}

			return deptDto;
		}
		catch (Exception ex) {
			throw new SystemException(rb.getString(GuiConstants.MASTER_VIEW_EDIT_DEPARTMENT_EXCEPTION));
		}
	}//end getDepartment

	/**
	 * Method called to get the Department based on the passed Code
	 * @param _deptCode
	 * @return
	 * @throws Exception
	 */
	@Override
	public DeptDto getDepartmentOnCode(final String _code) throws Exception {
		final DepartmentDao _deptDao = new DepartmentDaoImpl();
		DeptDto _deptDto = new DeptDto();
		_deptDto.setCode(_code);

		DeptDto deptDto = null;
		List<DeptDto> _deptList = _deptDao.getDepartments(_deptDto);
		if(_deptList != null && !_deptList.isEmpty()){
			deptDto = _deptList.get(0);
		}

		return deptDto;
	}//end getDepartmentOnCode

	/**
	 * Method called to get the Department based on the passed Department Object
	 * @param deptDto
	 * @return
	 * @throws Exception
	 */
	@Override
	public DeptDto getDepartment(final DeptDto _deptDto) throws Exception {
		final DepartmentDao _deptDao = new DepartmentDaoImpl();

		DeptDto deptDto = null;
		List<DeptDto> _deptList = _deptDao.getDepartments(_deptDto);
		if(_deptList != null && !_deptList.isEmpty()){
			deptDto = _deptList.get(0);
		}

		return deptDto;
	}//end getDepartments

	/**
	 * Method called to get the list of Department based on the passed Department Object
	 * @param deptDto
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<DeptDto> getDepartments(final DeptDto _deptDto) throws Exception {
		final DepartmentDao _deptDao = new DepartmentDaoImpl();
		return _deptDao.getDepartments(_deptDto);
	}//end getDepartments

	/**
	 * Method called to search for the Departments based on the passed Department Object
	 * @param deptDto
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<DeptDto> searchDepartments(DeptDto _deptDto) throws Exception {
		try{
			final DepartmentDao _deptDao = new DepartmentDaoImpl();
			return _deptDao.searchDepartments(_deptDto);
		}
		catch (Exception ex) {
			throw new SystemException(rb.getString(GuiConstants.MASTER_VIEW_MAINTAIN_DEPARTMENT_EXCEPTION));
		}
	}//end searchDepartments



}
